package com.zlc.common.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class PinyinUtils {

    private PinyinUtils(){}

    public static String pinyin(String hanzi){
        HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
        outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        StringBuilder builder = new StringBuilder();
        for(int i =0 ; i<hanzi.length() ; i++){
            char c = hanzi.charAt(i);
            try {
                String[] strings = PinyinHelper.toHanyuPinyinStringArray(c, outputFormat);
                builder.append(strings[0]);
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
        }
        return builder.toString();
    }

}
